from enum import Enum

from django.db import models

# Create your models here.
from utils.func import get_enum_value
from utils.models.base_manager import BaseDBManager


# 模型基类
class BaseModel(models.Model):
    id = models.AutoField(primary_key=True)
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="修改时间")

    objects = BaseDBManager()

    class Meta:
        abstract = True


# 文章类
class BlogArticle(BaseModel):
    articles_title = models.CharField(max_length=64)  # 文章标题
    articles_image_url = models.CharField(max_length=255)  # 文章封面
    categorie = models.CharField(max_length=64)  # 文章分类
    articles_type = models.CharField(max_length=64, default="0")  # 文章类型(0:"原创",1:"转载",2:"翻译")',
    articles_reprint_url = models.TextField(default="")  # 文章转载地址
    articles_status = models.CharField(max_length=64, default='0')  # 文章状态 文章状态(0:"发布",1:"回收站",2:"隐藏")
    articles_is_topping = models.CharField(max_length=8, default='0')  # 文章是否置顶
    word_count = models.IntegerField()  # 文章字数
    link_number = models.IntegerField(default=0)  # 点赞数量
    reading_time = models.CharField(max_length=64)  # 阅读时间
    reading_quantity = models.IntegerField(default=0)  # 阅读量
    comment_count = models.IntegerField(default=0)  # 评论数量
    articles_content = models.TextField()  # 文章内容
    article_describe = models.CharField(max_length=255)
    # 文章表更新时间自动更新
    update_time = models.DateTimeField(auto_now_add=True, verbose_name="修改时间")
    class Meta:
        managed = False
        db_table = 'blog_article'


# 用户表
class BlogUser(BaseModel):
    class ArticlesStatusEnum(Enum):
        SUPER_ADMIN = (0, 'super_admin')
        ADMIN = (1, 'admin')
        USER = (2, 'user')

    username = models.CharField(max_length=128)
    password = models.CharField(max_length=128)
    user_type = models.CharField(max_length=128)
    blog_name = models.CharField(max_length=32)
    user_image_url = models.TextField()
    motto = models.CharField(max_length=255)
    email = models.CharField(max_length=255, )
    qq_account = models.CharField(max_length=255)
    github_link = models.CharField(max_length=255)
    gitee_link = models.CharField(max_length=255)
    about = models.TextField(default="")

    @staticmethod
    def get_user_type(user_type):
        return get_enum_value(BlogUser.ArticlesStatusEnum, user_type)

    class Meta:
        managed = False
        db_table = 'blog_user'


# 标签
class BlogTag(BaseModel):
    tag_name = models.CharField(max_length=255, verbose_name="标签名称")
    articles_count = models.IntegerField(default=0, verbose_name="文章数量")

    class Meta:
        managed = False
        db_table = 'blog_tag'


# 标签文章表
class BlogTagArticle(models.Model):
    id = models.AutoField(primary_key=True)
    tag_id = models.IntegerField()
    article_id = models.IntegerField()

    class Meta:
        managed = False
        db_table = 'blog_tag_article'


# 分类表
class BlogCategorie(BaseModel):
    categorie_name = models.CharField(max_length=255)
    categorie_count = models.IntegerField(default=0)

    class Meta:
        managed = False
        db_table = 'blog_categorie'


# 友链表
class BlogLink(BaseModel):
    link_name = models.CharField(max_length=255)
    link_image_url = models.TextField()
    link_url = models.TextField()
    desc = models.CharField(max_length=255)

    class Meta:
        managed = False
        db_table = 'blog_link'


# 音乐表
class BlogMusics(BaseModel):
    music_cover = models.TextField()
    music_artist = models.CharField(max_length=255)
    music_name = models.CharField(max_length=255)
    music_url = models.TextField()

    class Meta:
        managed = False
        db_table = 'blog_musics'


# 页面管理
class BlogPages(BaseModel):
    page_name = models.CharField(max_length=255)
    page_label = models.CharField(max_length=255)
    background_image_url = models.TextField()

    class Meta:
        managed = False
        db_table = 'blog_pages'


# 网站信息表
class BlogWebInfo(models.Model):
    id = models.AutoField(primary_key=True)
    total_view = models.IntegerField()
    notice = models.CharField(max_length=255, default='暂无公告')
    website_title = models.CharField(max_length=255)
    article_count = models.IntegerField()
    tag_count = models.IntegerField()
    cateorie_count = models.IntegerField()
    website_records = models.CharField(max_length=255)
    website_create_time = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'blog_webinfo'


# 草稿表
class BlogDraft(models.Model):
    id = models.AutoField(primary_key=True)
    draft_title = models.CharField(max_length=255)
    draft_data = models.TextField()
    create_time = models.DateTimeField(auto_now_add=True)

    objects = BaseDBManager()

    class Meta:
        managed = False
        db_table = 'blog_draft'


# 留言类
class BlogMessage(models.Model):
    id = models.AutoField(primary_key=True)
    blog_message = models.CharField(max_length=255)  # 留言内容
    audit = models.IntegerField(default=0)  # 审核状态 0:待审核 1:审核通过 2:审核不通过'
    create_time = models.DateTimeField(auto_now_add=True)

    objects = BaseDBManager()

    class Meta:
        managed = False
        db_table = 'blog_message'


# 评论类
class BlogComments(models.Model):
    id = models.AutoField(primary_key=True)
    article_id = models.CharField(default='', max_length=255)  # 文章id
    article_name = models.CharField(default='', max_length=255)  # 文章名称
    father_id = models.CharField(default="", max_length=255)  # 父评论id
    reply_id = models.CharField(default='', max_length=255)  # 回复id
    reply_name = models.CharField(default='', max_length=255)  # 回复名称
    title_image = models.CharField(default='', max_length=255)  # 头像
    name = models.CharField(max_length=255)  # 评论人名称
    email = models.CharField(max_length=255)  # 评论人邮箱
    content = models.CharField(max_length=255)  # 评论内容
    comments_like = models.IntegerField(default=0)  # 评论喜欢数量
    comments_type = models.IntegerField(default=0)  # 评论类型 '(文章:0,友链:1)
    is_master = models.IntegerField(default=0)  # 是否博主评论
    create_time = models.DateTimeField(auto_now_add=True)  # 创建时间

    objects = BaseDBManager()

    class Meta:
        managed = False
        db_table = 'blog_comments'


# 博客访问统计
class BlogVisits(models.Model):
    visits_date = models.DateField(primary_key=True)
    visits_count = models.IntegerField(default=1)

    class Meta:
        managed = False
        db_table = 'blog_visits'
